<?php
include_once('../includes/definer.php');
include_once(INC.'php/bootstrap.php');

$categoria = ($_POST['id'])?Doctrine::getTable('categoria')->find($_POST['id']):new Categoria();
$categoria->value = $_POST['value'];
$categoria->save();

if (isset($_POST['imgprefix'])) {
    $i=1;
    foreach (glob(INC.'../content/tmp/categorias/'.$_POST['imgprefix'].'-*') as $img) {
        $imageName = str_replace(INC.'../content/tmp/categorias/', '', $img);
        $imageModel = Doctrine::getTable('imagen')->findOneBySrc($imageName);
        $imageModel->src = $categoria->slug.'.'.$i.'.jpg';
        $categoria->banner = $imageModel;
        $categoria->save();
        rename(INC.'../content/tmp/categorias/'.$imageName, INC.'../content/categorias/'.$categoria->slug.'.'.$i.'.jpg');
        $i++;
    }
}

if ($categoria->slug == 'prensa') {
    header('location: '.URL.'notas#portada');
    exit();
}

if (isset($_POST['redirect'])) {
    $accion = ($_POST['id'])?'#edit':'#new';
    header('location: '.URL.'categorias'.$accion);
}
?>
